#!/usr/bin/python

CONFIG_FILE = 'config.txt'
POLL_INTERVAL = 0.25
ACCOUNT_NO = 2 


import sys
import time
import xmpp


def read_ConfigFile():
	"Read configuration settings for all accounts"
	global configs
	configs = {}
	for line in open(CONFIG_FILE).readlines():
		line = line.strip()
		if len(line) > 0 and not line.startswith('#'):	# Not a blank line or a comment
			key, value = line.split(':')
			key = key.strip().lower()
			value = value.strip()
			if configs.has_key(key):
				configs[key].append(value);
			else:
				configs[key] = [value]


def start_Connection():
	global client, roster

	domain = configs['domain'][ACCOUNT_NO]
	username = configs['username'][ACCOUNT_NO]
	password = configs['password'][ACCOUNT_NO]
	url = configs['url'][ACCOUNT_NO]
	port = int(configs['port'][ACCOUNT_NO])

	print 'ACTION: connecting %s@%s to server' % (username, domain)
	client = xmpp.Client(domain, debug=[])
	client.connect(server = (url, port))
	client.auth(username, password)
	client.sendInitPresence()
	roster = client.getRoster()



def subscribe_Contacts(contactFile):
	global roster
	count = 0
	for filename in open(contactFile).readlines():
		contact = filename.strip()
		if contact[0] in ['l', 'p']:
			count += 1
#			print 'ACTION: subscribing to', contact
#			roster.Subscribe(contact)
#			time.sleep(POLL_INTERVAL)
	print count

def email_Contacts(contactFile):
	for filename in open(contactFile).readlines():
		filename = filename.strip()
		

if __name__ == '__main__':
	if len(sys.argv) == 2:
		read_ConfigFile()
		start_Connection()
		subscribe_Contacts(sys.argv[1])
#		email_Contacts(sys.argv[1])		
	else:
		print 'USAGE:'
